package com.example.widgetapplication;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class SurveyActivity extends AppCompatActivity {
    private TextView title1,title2,title3,title4,title5,title6,title7,title8;
    private String topic1,topic2,topic3,topic4,topic5,topic6,topic7,topic8,result_1,result_2,result_3,result_4,result_5,result_6,result_7,result_8,result_9,result_10,result_11,result_12;
    private RadioGroup radioGroup1,radioGroup2,radioGroup3;
    private CheckBox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6,checkBox7,checkBox8,checkBox9,checkBox10,checkBox11,checkBox12,checkBox13,checkBox14,checkBox15,checkBox16,checkBox17,checkBox18,checkBox19,checkBox20;
    private EditText editText1,editText2,editText3,editText4,editText5;
    private ImageButton imageButton;
    private List<CheckBox> checkBoxList1 = new ArrayList<CheckBox>();
    private List<CheckBox> checkBoxList2 = new ArrayList<CheckBox>();
    private List<CheckBox> checkBoxList3 = new ArrayList<CheckBox>();
    private List<CheckBox> checkBoxList4 = new ArrayList<CheckBox>();


    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_survey);

        /*获取题目信息*/
        title1 = findViewById(R.id.Title1);
        topic1 = title1.getText().toString();

        title2 = findViewById(R.id.Title2);
        topic2 = title2.getText().toString();

        title3 = findViewById(R.id.Title3);
        topic3 = title3.getText().toString();

        title4 = findViewById(R.id.Title4);
        topic4 = title4.getText().toString();

        title5 = findViewById(R.id.Title5);
        topic5 = title5.getText().toString();

        title6 = findViewById(R.id.Title6);
        topic6 = title6.getText().toString();

        title7 = findViewById(R.id.Title7);
        topic7 = title7.getText().toString();

        title8 = findViewById(R.id.Title8);
        topic8 = title8.getText().toString();

        /*获取单选结果*/
        radioGroup1 = findViewById(R.id.question_title1);
        radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.aq1:
                        result_1="男";
                        break;
                    case R.id.aq2:
                        result_1="女";
                        break;
                    default:
                        result_1="无效选项";
                        break;
                }
            }
        });

        radioGroup2 = findViewById(R.id.question_title2);
        radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.bq1:
                        result_2="朋友介绍";
                        break;
                    case R.id.bq2:
                        result_2="广告";
                        break;
                    case R.id.bq3:
                        result_2="软件商店推荐";
                        break;
                    case R.id.bq4:
                        result_2="其他";
                        break;
                    default:
                        result_2="无效选项";
                        break;
                }
            }
        });

        radioGroup3 = findViewById(R.id.question_title3);
        radioGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.cq1:
                        result_3="1小时左右";
                        break;
                    case R.id.cq2:
                        result_3="5小时左右";
                        break;
                    case R.id.cq3:
                        result_3="8小时左右";
                        break;
                    case R.id.cq4:
                        result_3="9小时以上";
                        break;
                    default:
                        result_3="无效选项";
                        break;
                }
            }
        });

        /*多选*/
        checkBox1 = findViewById(R.id.dqs1);
        checkBox2 = findViewById(R.id.dqs2);
        checkBox3 = findViewById(R.id.dqs3);
        checkBox4 = findViewById(R.id.dqs4);

        checkBoxList1.add(checkBox1);
        checkBoxList1.add(checkBox2);
        checkBoxList1.add(checkBox3);
        checkBoxList1.add(checkBox4);

        editText1 =findViewById(R.id.txt_content1);

        checkBox5 = findViewById(R.id.eqs1);
        checkBox6 = findViewById(R.id.eqs2);
        checkBox7 = findViewById(R.id.eqs3);
        checkBox8 = findViewById(R.id.eqs4);
        checkBox9 = findViewById(R.id.eqs5);
        checkBox10 = findViewById(R.id.eqs6);

        checkBoxList2.add(checkBox5);
        checkBoxList2.add(checkBox6);
        checkBoxList2.add(checkBox7);
        checkBoxList2.add(checkBox8);
        checkBoxList2.add(checkBox9);
        checkBoxList2.add(checkBox10);

        editText2 =findViewById(R.id.txt_content2);

        checkBox11 = findViewById(R.id.fqs1);
        checkBox12 = findViewById(R.id.fqs2);
        checkBox13 = findViewById(R.id.fqs3);
        checkBox14 = findViewById(R.id.fqs4);
        checkBox15 = findViewById(R.id.fqs5);

        checkBoxList3.add(checkBox11);
        checkBoxList3.add(checkBox12);
        checkBoxList3.add(checkBox13);
        checkBoxList3.add(checkBox14);
        checkBoxList3.add(checkBox15);

        editText3 =findViewById(R.id.txt_content3);

        checkBox16 = findViewById(R.id.gqs1);
        checkBox17 = findViewById(R.id.gqs2);
        checkBox18 = findViewById(R.id.gqs3);
        checkBox19 = findViewById(R.id.gqs4);
        checkBox20 = findViewById(R.id.gqs5);

        checkBoxList4.add(checkBox16);
        checkBoxList4.add(checkBox17);
        checkBoxList4.add(checkBox18);
        checkBoxList4.add(checkBox19);
        checkBoxList4.add(checkBox20);

        editText4 =findViewById(R.id.txt_content4);

        /*提交按钮调研结果并打印*/
        editText5 =findViewById(R.id.txt_content5);
        imageButton = findViewById(R.id.button_sumit);

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuffer stringBuffer1 = new StringBuffer();
                for (CheckBox checkbox : checkBoxList1) {
                    if (checkbox.isChecked()) {
                        stringBuffer1.append(checkbox.getText().toString()+" ");
                    }
                }
                result_4 = stringBuffer1.toString();
                result_5 = editText1.getText().toString();

                StringBuffer stringBuffer2 = new StringBuffer();
                for (CheckBox checkbox : checkBoxList2) {
                    if (checkbox.isChecked()) {
                        stringBuffer2.append(checkbox.getText().toString()+" ");
                    }
                }
                result_6 = stringBuffer2.toString();
                result_7 = editText2.getText().toString();

                StringBuffer stringBuffer3 = new StringBuffer();
                for (CheckBox checkbox : checkBoxList3) {
                    if (checkbox.isChecked()) {
                        stringBuffer3.append(checkbox.getText().toString()+" ");
                    }
                }
                result_8 = stringBuffer3.toString();
                result_9 = editText3.getText().toString();

                StringBuffer stringBuffer4 = new StringBuffer();
                for (CheckBox checkbox : checkBoxList4) {
                    if (checkbox.isChecked()) {
                        stringBuffer4.append(checkbox.getText().toString()+" ");
                    }
                }
                result_10 = stringBuffer4.toString();
                result_11 = editText4.getText().toString();

                result_12 = editText5.getText().toString();

                Log.d("SurveyActivity",topic1 + " " + result_1);
                Log.d("SurveyActivity",topic2 + " " + result_2);
                Log.d("SurveyActivity",topic3 + " " + result_3);
                Log.d("SurveyActivity",topic4 + " " + result_4 + " " + result_5);
                Log.d("SurveyActivity",topic5 + " " + result_6 + " " + result_7);
                Log.d("SurveyActivity",topic6 + " " + result_8 + " " + result_9);
                Log.d("SurveyActivity",topic7 + " " + result_10 + " " + result_11);
                Log.d("SurveyActivity",topic8 + " " + result_12);

                switch (view.getId()){
                    case R.id.button_sumit:
                        Toast.makeText(SurveyActivity.this,"提交成功",Toast.LENGTH_SHORT).show();/*显示弹窗*/
                        break;
                    default:
                        break;
                }

            }
        });


    }
}